package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.admin.service.ILegalCaseOrderService;
import com.mdd.admin.validate.LegalCaseOrderCreateValidate;
import com.mdd.admin.validate.LegalCaseOrderSearchValidate;
import com.mdd.admin.validate.LegalCaseOrderUpdateValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.LegalCaseOrderDetailVo;
import com.mdd.admin.vo.LegalCaseOrderListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.LegalCaseOrder;
import com.mdd.common.mapper.LegalCaseOrderMapper;
import com.mdd.common.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 案件订单实现类
 * @author LikeAdmin
 */
@Service
public class LegalCaseOrderServiceImpl implements ILegalCaseOrderService {
        
    @Resource
    LegalCaseOrderMapper legalCaseOrderMapper;

    /**
     * 案件订单列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<LegalCaseOrderListedVo>
     */
    @Override
    public PageResult<LegalCaseOrderListedVo> list(PageValidate pageValidate, LegalCaseOrderSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        MPJQueryWrapper<LegalCaseOrder> mpjQueryWrapper = new MPJQueryWrapper<>();
        mpjQueryWrapper.selectAll(LegalCaseOrder.class)
                .select("t2.case_name as caseName, t3.area_name as areaName, " +
                        "t4.real_name as userName, t5.real_name as lawyerName, t2.case_trade_no as caseNo")
                .leftJoin("la_user t4 on t4.id = t.user_id")
                .leftJoin("legal_case t2 on t2.id = t.case_id")
                .leftJoin("legal_area_city t3 on t3.area_id = t2.area_id")
                .leftJoin("la_user t5 on t5.id = t2.lawyer_id")
                .orderByDesc("t.id");

        if (StringUtils.isNotEmpty(searchValidate.getKeyword())) {
            mpjQueryWrapper.nested(wq -> wq
                    .like("t4.real_name", searchValidate.getKeyword()).or()
                    .like("t5.real_name", searchValidate.getKeyword()));
        }
        IPage<LegalCaseOrderListedVo> iPage = legalCaseOrderMapper.selectJoinPage(
                new Page<>(page, limit), LegalCaseOrderListedVo.class, mpjQueryWrapper);
        for (LegalCaseOrderListedVo vo : iPage.getRecords()) {
            if (vo.getPayTime() != null) {
                vo.setPayTimeStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(vo.getPayTime() * 1000)));
            }
        }

        return PageResult.iPageHandle(iPage);
    }

    /**
     * 案件订单详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return LegalCaseOrder
     */
    @Override
    public LegalCaseOrderDetailVo detail(Integer id) {
        LegalCaseOrder model = legalCaseOrderMapper.selectOne(
                new QueryWrapper<LegalCaseOrder>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        LegalCaseOrderDetailVo vo = new LegalCaseOrderDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 案件订单新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(LegalCaseOrderCreateValidate createValidate) {
        LegalCaseOrder model = new LegalCaseOrder();
        model.setAId(createValidate.getAId());
        model.setCaseId(createValidate.getCaseId());
        model.setUserId(createValidate.getUserId());
        model.setOutTradeNo(createValidate.getOutTradeNo());
        model.setType(createValidate.getType());
        model.setPayType(createValidate.getPayType());
        model.setAmount(createValidate.getAmount());
        model.setFyAmount(createValidate.getFyAmount());
        model.setAreasFyAmount(createValidate.getAreasFyAmount());
        model.setLawyerFyAmount(createValidate.getLawyerFyAmount());
        model.setPartnerFyAmount(createValidate.getPartnerFyAmount());
        model.setPayTime(createValidate.getPayTime());
        model.setStatus(createValidate.getStatus());
        model.setTransactionId(createValidate.getTransactionId());
        model.setPrepayId(createValidate.getPrepayId());
        model.setNewlandTradeNo(createValidate.getNewlandTradeNo());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        legalCaseOrderMapper.insert(model);
    }

    /**
     * 案件订单编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(LegalCaseOrderUpdateValidate updateValidate) {
        LegalCaseOrder model = legalCaseOrderMapper.selectOne(
                new QueryWrapper<LegalCaseOrder>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setAId(updateValidate.getAId());
        model.setCaseId(updateValidate.getCaseId());
        model.setUserId(updateValidate.getUserId());
        model.setOutTradeNo(updateValidate.getOutTradeNo());
        model.setType(updateValidate.getType());
        model.setPayType(updateValidate.getPayType());
        model.setAmount(updateValidate.getAmount());
        model.setFyAmount(updateValidate.getFyAmount());
        model.setAreasFyAmount(updateValidate.getAreasFyAmount());
        model.setLawyerFyAmount(updateValidate.getLawyerFyAmount());
        model.setPartnerFyAmount(updateValidate.getPartnerFyAmount());
        model.setPayTime(updateValidate.getPayTime());
        model.setStatus(updateValidate.getStatus());
        model.setTransactionId(updateValidate.getTransactionId());
        model.setPrepayId(updateValidate.getPrepayId());
        model.setNewlandTradeNo(updateValidate.getNewlandTradeNo());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        legalCaseOrderMapper.updateById(model);
    }

    /**
     * 案件订单删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        LegalCaseOrder model = legalCaseOrderMapper.selectOne(
                new QueryWrapper<LegalCaseOrder>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        legalCaseOrderMapper.delete(new QueryWrapper<LegalCaseOrder>().eq("id", id));
    }

}
